
package cn.lijigou.shopping;

import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.service.ServerService;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import cn.lijigou.shopping.util.StringTool;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 董永健 E-mail:dong_yong_jian@163.com
 * @version CreatTime：2011-3-31 下午04:26:55
 * 类说明
 */
public class ProposeActivity extends MActivity implements OnClickListener{
	private final int SUBMIT_START_ID = 1;
	private final int SUBMIT_END_ID = 2;
	public  String id = "";
	public  String content ="";
	public  String email ="";
	private EditText propose_content = null;
	private EditText propose_email = null;
	private TextView back= null;
	private LinearLayout titleLeftView= null;
	private TextView button_submit = null;
	private LinearLayout titleRightView = null;
	private TextView title_center = null;
	private ProgressDialog mProgress;
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	public Handler resultHander = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what==SUBMIT_START_ID){
				Toast.makeText(ProposeActivity.this,R.string.thank_you_for_your_feedback, 1).show();
				ProposeActivity.this.finish();
			}
			if(msg.what==SUBMIT_END_ID){
				Toast.makeText(ProposeActivity.this,R.string.failure_to_submit, 1).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.propose);
		overridePendingTransition(R.anim.push_in,R.anim.push_out);
		setViews();
		setOnClickListeners();
		
	}
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}



	private void  setViews(){
		back = (TextView) this.findViewById(R.id.title_btn_left);
		titleLeftView = (LinearLayout) this.findViewById(R.id.titleLeftView);
		back.setBackgroundResource(R.drawable.title_back_normal);
		
		button_submit = (TextView) this.findViewById(R.id.title_btn_right);
		titleRightView = (LinearLayout) this.findViewById(R.id.titleRightView);
		button_submit.setText(R.string.submit);
		title_center = (TextView) this.findViewById(R.id.text_center);
		title_center.setText(R.string.feedback);
		
		propose_content = (EditText) findViewById(R.id.propose_content);
		propose_email = (EditText) findViewById(R.id.propose_email);
	}
	
	private void  setOnClickListeners(){
		titleLeftView.setOnClickListener(this);
		titleRightView.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		MSystem.pv += 1;
		switch (v.getId()) {
		case R.id.titleRightView: // 路客意见
			content = propose_content.getText().toString() + "@Device=" + Build.MODEL + "@Version=" + Build.VERSION.RELEASE
					+ "@SoftWareVersion=" + getVersionName();
			email = propose_email.getText().toString();
			if (StringTool.isNull(propose_content.getText().toString())) {
				Toast.makeText(this, R.string.thank_you_for_the_air_support, 1).show();
			} else {
				executorService.execute(new Thread() {
					public void run() {
						showProgress();
						Boolean mBoolean = MSystem.setPropose(content + "::" + email).isSuccess();
						if (mBoolean != null) {
							if (mBoolean) {
								resultHander.sendEmptyMessage(SUBMIT_START_ID);
							} else {
								resultHander.sendEmptyMessage(SUBMIT_END_ID);
							}
						}
						hideProgress();
					}
				});

			}
			break;
		case R.id.titleLeftView:
			this.finish();
			break;
		}
	}
	
    /**
     * 
     * 获取当前程序的版本号 　　
     */
    private String getVersionName(){
    	
    		String version ="";
            // 获取packagemanager的实例
            PackageManager packageManager = getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo;
			try {
				packInfo = packageManager.getPackageInfo(getPackageName(),0);
				version = packInfo.versionName;
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
            return version;
    }
    
    /**
	 * 显示等待框
	 * 
	 * @param title
	 * @param message
	 */
	protected void showProgress() {
		this.runOnUiThread(new Runnable() {
			

			public void run() {	
				mProgress = new ProgressDialog(ProposeActivity.this);
				mProgress.requestWindowFeature(Window.FEATURE_NO_TITLE);
				mProgress.setMessage("正在提交，请稍等...");
				mProgress.show();
			}
		});
	}

	/**
	 * 取消等待框
	 */
	protected void hideProgress() {
		
		this.runOnUiThread(new Runnable() {
			public void run() {
				if (mProgress != null) {
					mProgress.dismiss();
				}
			}
		});
	}
}
